#Usage: weaver.py rc-gcrrt.py full_directory_path num_rrts
#Programmer: Rory Carmichael
#Purpose: generate makeflow for GC content conserving random reverse translations

import shlex, subprocess
from weaver.nest import Nest
from weaver.function import PythonFunction, Function
from array import array
import re
import sys

if (len(CurrentScript().arguments) != 2) :
	print "Usage: weaver.py weaver_rrt_dir.py full_directory_path num_rrts\n"
	sys.exit(1)

make_ci = Function('rc-count-codon-gc3-bins.pl', cmd_format = '{executable} {inputs} {outputs}')
make_rrt = Function('rc-gc3-rrt-gen.pl', cmd_format = '{executable} {inputs} {outputs}')
tar = Function('tar', cmd_format = '{executable} -czf {outputs} {inputs} --remove-files')

files = Glob(str(CurrentScript().arguments[0])+"/*.fasta")
num_rrts = int(CurrentScript().arguments[1])

for i in files:
	ci_out = str(i) + ".counts"
	tar_out = str(i) + ".tgz"
	make_ci(inputs=i,outputs=ci_out)
	rrt_list = []
	for j in range(num_rrts):
		curout = str(i) + "." + str(j) + ".fasta"
		curstat = str(i) + "." + str(j) + ".stat"
		rrt_list.append(curout)
		rrt_list.append(curstat)
		make_rrt(inputs=[i,ci_out], outputs=[curstat,curout])
	rrt_list.append(ci_out)
	tar(inputs=rrt_list,outputs=tar_out, collect=True, local=True)
